package com.itheima.web.controller.system;

import com.itheima.common.utils.UtilFuns;
import com.itheima.doamin.system.Module;
import com.itheima.service.system.ModuleService;
import com.itheima.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/system/module/")
public class ModuleController extends BaseController{
    @Autowired
    ModuleService moduleService;
    //全部
    @RequestMapping("list")
    public String findAll(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "3") int size){
        request.setAttribute("page",moduleService.findAll(page,size));
        return "system/module/module-list";
    }

    //更新
    @RequestMapping("toUpdate")
    public String update(String id){
        //回显
        request.setAttribute("module",moduleService.findById(id));
        request.setAttribute("menus",moduleService.findAll());
        return "system/module/module-update";
    }

    //添加
    @RequestMapping("toAdd")
    public String toAdd(){
        //回显
        request.setAttribute("menus",moduleService.findAll());
        return "system/module/module-add";
    }

    //更新添加
    @RequestMapping("edit")
    public String edit(Module module){
        if (UtilFuns.isNotEmpty(module.getId())){
            //不为空
            moduleService.update(module);
        }else {
            //为空
            module.setId(UtilFuns.getUUID());
            moduleService.save(module);
        }
        return "redirect:/system/module/list";
    }

    //删除
    @RequestMapping("delete")
    public String delete(String id){
        moduleService.delete(id);
        return "redirect:/system/module/list";
    }

}
